package com.xwli.springcloud.controller;


import com.alibaba.fastjson.JSON;
import com.xwli.springcloud.projo.Dept;
import com.xwli.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ConsumerController {

    @Autowired
    private DeptClientService deptClientService;

    @RequestMapping("/dept/list")
    public List<Dept> getDeptList(){
        return deptClientService.queryAll();
    }

    /**
     * 添加一个部门信息
     * @param dept
     * @return
     */
    @RequestMapping("/dept/add")
    public boolean addDept(Dept dept){
        String json = JSON.toJSONString(dept);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> stringHttpEntity = new HttpEntity<>(json, httpHeaders);
        Dept dept1 = dept;
        return deptClientService.addDept(dept);
    }

    /**
     * 根据部门编号获取信息
     * @param id
     * @return
     */
    @RequestMapping("/dept/get/{id}")
    public Dept getDeptById(@PathVariable(name = "id")Long id){
        return deptClientService.queryById(id);
    }
}
